package com.gxkj.movie.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxkj.movie.config.utils.OkHttpCli;
import com.gxkj.movie.controllers.seo.IndexSeo;
import com.gxkj.movie.helpers.*;
import com.gxkj.movie.threads.BaiduSubmitUrlThread;
import io.micrometer.core.instrument.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author sqyc
 */
@Controller
@Slf4j
public class IndexController extends BaseController {

    @Autowired
    private MovSiteHelpper movSiteHelpper;



    @Autowired
    private BaiduSpiderHelper baiduSpiderHelper;

    String[] colors = new String[]{
            "/static/img/color/blue.png",
            "/static/img/color/green.png"
//            ,"/static/img/color/pink.png"
            ,"/static/img/color/orange.png"
//            ,"/static/img/color/red.png"

    };
    String pink = "/static/img/color/pink.png";

    @GetMapping("")
    public String index(HttpServletRequest req, Model model,@RequestParam(value = "forceclear" ,required=false,defaultValue ="false") Boolean forceclear){



        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
        ZonedDateTime zdt = ZonedDateTime.now();
        log.info(formatter.format(zdt)+"请求首页");
        String basePath = req.getScheme()+"://"+req.getServerName()+"/";

        String[] param = {
                //需要推送的网址
                basePath
        };

        new Thread(new BaiduSubmitUrlThread(baiduSpiderHelper,param)).start();

        clckTime ++;
        cacheContol(req,model,forceclear);
        renderHeader(  req,   model);

        //推荐视频
        renderRecommendVod( req,   model);
        //新闻提示
        renderNews(req,   model,null);



//        renderJdProd(req,   model,  "taobao_nvbao","淘宝热销女包");
//        renderJdProd(req,   model,  "taobao_nvzhuang","淘宝热销女装");
//
//        renderJdProd(req,   model,  "taobao_nvxie","淘宝热销女鞋");
//
//        renderJdProd(req,   model,  "jd_pro","京东热销商品");
//        renderJdProd(req,   model,  "jd_huodong","京东最近热门活动");

        showMovieNavData(  req,   model);

        return getTempletate(req)+"/indexv3";
    }

    @Override
    public void renderHeader(HttpServletRequest req, Model model){
        super.renderHeader(req,model);
        model.addAttribute("menu", MenuEnum.INDEX.getKey());
        model.addAttribute("TITLE", IndexSeo.TITLE);
        model.addAttribute("KEYWORDS",IndexSeo.KEYWORDS);
        model.addAttribute("DESCRIPTION",IndexSeo.DESCRIPTION);
        model.addAttribute("SITE_NAME", IndexSeo.SITE_NAME);
    }


    protected void showMovieNavData(HttpServletRequest req, Model model){

        StopWatch sw = new StopWatch("test");
        if(dataList.isEmpty()){
            sw.start("请求数据");
            String key = "";
            int pageSize = 60;
            JSONObject pager = movSiteHelpper.findPageData(1,pageSize,key);


            if(pager != null){
                JSONArray list = pager.getJSONArray("list");
                if(dataList != null ){
                    dataList.addAll(list);
                    int total = pager.getInteger("total");
                    int   countPage = total / pageSize;
                    if (total % pageSize != 0){
                        countPage ++;
                    }
                    for(int i=2;i<=countPage;i++){
                        pager = movSiteHelpper.findPageData(i,pageSize,key);
                        if(pager != null){
                            list = pager.getJSONArray("list");
                            if(dataList != null ){
                                dataList.addAll(list);
                            }
                        }
                    }
                }


            }
            sw.stop();
            sw.start("组装icon");
            if(dataList != null ){
                JSONObject item = null;
                int length = colors.length;
                int r = 0;

                String tag = null;
                for(int i=0;i<dataList.size();i++){
                    item = dataList.getJSONObject(i);
                    r = i%length;
                    tag = item.getString("tag");
                    if(org.springframework.util.StringUtils.isEmpty(tag)){
                        item.put("icon",colors[r]);
                    }else {
                        if((","+tag+",").indexOf(",2,") >= 0){
                            item.put("icon",pink);
                        }else{
                            item.put("icon",colors[r]);
                        }
                    }


                }
            }
            sw.stop();
        }
        if(sw.isRunning()){
            sw.stop();
        }
        //log.info("a--"+sw.prettyPrint());

        log.info("a--"+sw.shortSummary()+" "+sw.getTotalTimeMillis());
        model.addAttribute("dataList",dataList);
    }
}
